package ht

// 注释
/*
ver
*/


object EasyScala {
  def mute(ps: Any*): Unit = {}

  def vari():Unit = {
    println("变量常量")
    var a = "a"
    val b = "b"
    a = "b"

    mute(a, b)
  }

  def stri():Unit = {
    println("字符串")
    val a = "a\nb"
    val b = "a${a}b"
    val c = """
      a
      b
    """.trim()
  }

  def coll():Unit = {
    println("数组集合")
    val tup = (1, 2.0, "3")
    val arr = Array(1, 2, 3)
    val list1 = List(1, 2, 3, "cde")
    val map = Map("a" -> 1, "b" -> 2)
    val map2 = new scala.collection.mutable.HashMap[String, Int]()

    map2("a") = 1

    mute(arr(0), list1.head, map.head._1)

    for (arg <- arr) {
      mute(arg)
    }

    for (i <- 0 until 2) {
      mute(i) //0, 1
    }

    arr.foreach { i =>
      mute(i)
    }

    val list2 = arr.toList.map { i =>
      i + 1
    }

    val map3 = map.map(e => {
      (e._1, e._2 + 1)
    })

    mute(list2)
    mute(tup._1)
  }

  def proc():Unit = {
    println("流程控制")

    if (1 == 1) {}

    val a = if (1 == 1) {
      1
    } else {
      0
    }

    var n = 2
    while (n > 0) {
      n -= 1
    }

    val Pattern = "(s.*)".r
    val v1 = "spark"

    val r = v1 match {
      case "2"       => "2"
      case "1" | "3" => "1"
      case s: String => s
      case _         => "default"
      // case v if v.startsWith("s") => "s*"
      // case Pattern(v1) => "begin s*"
    }

    mute(r)
  }

  def func():Unit = {
    println("函数方法")
    def f(a: Int, b: Int = 1) = a + b
    val f2 = (a: Int, b: Int) => a + b
    val f4 = { (a: Int, b: Int) =>
      a + b
    }

    val b = f(1, _: Int)
    b(2)

    f(1, 2)
    f2(1, 2)
  }

  def clas():Unit = {
    println("类和对象")
    class C(val a: Int, var b: String) extends B {
      def this(a: Int, b: String, c: Int) {
        this(a, b)
        this.c = c
      }
      var c: Int = 1
      def f(): String = {
        "c_f"
      }
    }

    object C {
      def apply(a: Int, b: String, c: Int) = new C(a, b, c)
    }

    val c = C(0, "c", 1) //隐式调用apply
  }

  def main(args: Array[String]):Unit = {
    println("Hello, World!")
    vari()
    stri()
    coll()
    proc()
    func()
    clas()
  }
}

trait B
trait A extends B {
  def log(msg: String):Unit = {
    EasyScala.mute(msg)
  }
}
